﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SalesOrderListForPaymentManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                var bll = mk.l.Sales_SalesOrderHeader.ToList();
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(h => h.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                //只取出StatusSubmit为2 即设计师已经提交到财务的订单列表
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text + " and StatusSubmit<>'1'";
                // this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");

            }
        }
        private void BindData()
        {
            var bll = mk.l.Sales_SalesOrderHeader.ToList();
            var ds = bll.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //显示客户类型
                Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
                var bllC = mk.l.Sales_Customer.ToList();
                Sales_Customer mC = bllC.Where(h => h.CustomerID == (int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["CustomerID"].ToString()))).FirstOrDefault();
                if (mC != null)
                {
                    lblCustomerName.Text = mC.CustomerName;
                }
                //显示设计师姓名
                Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
                var bllUser = mk.l.Accounts_Users.ToList();
                Accounts_Users mUser = bllUser.Where(h => h.UserName == (this.myGridView.DataKeys[e.Row.RowIndex].Values["Designer"].ToString())).FirstOrDefault();
                lblDesigner.Text = mUser.TrueName;

                //显示提交状态
                Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
                string strStatusSubmit = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusSubmit"].ToString();
                switch (strStatusSubmit)
                {
                    case "1":
                        lblStatusSubmit.Text = "初始添加";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusSubmit.Text = "修改中";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "3":
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusSubmit.Text = "未知";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示付款状态
                Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
                string strStatusPayment = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPayment"].ToString();
                switch (strStatusPayment)
                {
                    case "1":
                        lblStatusPayment.Text = "未付款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPayment.Text = "未付全款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Black;
                        break;
                    case "3":
                        lblStatusPayment.Text = "已付全款";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPayment.Text = "未知";
                        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示订货状态 同时根据订货状态 确定编辑链接是Creat还是Edit
                Label lblStatusPurchaseMaterial = (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
                HyperLink linkPurchaseOrderEdit = (HyperLink)e.Row.FindControl("linkPurchaseOrderEdit");
                string strStatusPurchaseMaterial = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPurchaseMaterial"].ToString();
                string SalesOrderID = this.myGridView.DataKeys[e.Row.RowIndex].Values["SalesOrderID"].ToString();
                switch (strStatusPurchaseMaterial)
                {
                    case "1":
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPurchaseMaterial.Text = "部分订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "3":
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPurchaseMaterial.Text = "未知";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示流程状态
                Label lblStatusAll = (Label)(e.Row.FindControl("lblStatusAll"));
                string strStatusAll = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusAll"].ToString();
                switch (strStatusAll)
                {
                    case "1":
                        lblStatusAll.Text = "未完成";
                        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusAll.Text = "已完成";
                        lblStatusAll.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusAll.Text = "未知";
                        lblStatusAll.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.InsertOnSubmit("style", "vnd.ms-excel.numberformat:@");

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {

        }
    }
}